/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package personal.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
//import personal.jaxb.Book;
import personal.jaxb.Books;
import personal.obj.Book;

/**
 *
 * @author PHATNBSE61057
 */
public class StAXParserAPI {

    public StAXParserAPI() {
    }
    FileInputStream inputFile = null;

    public List<Book> searchBookInXMLFile(String categoryID) {
        //FileInputStream inputFile = null;
        try {
            XMLInputFactory xif = XMLInputFactory.newInstance();
            File file = new File("C:\\Users\\TruongHoangLe\\Desktop\\XML Project\\XML_Project\\web\\xml\\books.xml");
//            File file = new File("./web/xml/books.xml");
            inputFile = new FileInputStream(file);
            XMLStreamReader reader = xif.createXMLStreamReader(inputFile);

            List<Book> result = new ArrayList<Book>();
            Book book = new Book();
            Books books=new Books();
            
            boolean foundBook = false;
            boolean bookIDCheck = false;
            boolean categoryIDCheck = false;
            boolean bookNameCheck = false;
            boolean bookLinkCheck = false;
            boolean bookThumbCheck = false;
            boolean bookImgCheck = false;
            boolean bookPriceCheck = false;
            boolean bookPagesCheck = false;
            boolean bookFormatCheck = false;
            boolean bookAuthorCheck = false;
            boolean bookPublishedDateCheck = false;
            boolean bookISBNCheck = false;
            boolean bookDetailedEditionCheck = false;
            boolean bookDescriptionCheck = false;
            boolean matchBook = false;

            while (reader.hasNext()) {
                int eventType = reader.getEventType();
                switch (eventType) {
                    case XMLStreamConstants.START_ELEMENT:
                        if (reader.getLocalName().equalsIgnoreCase("book")) {
                            foundBook = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookID")) {
                            bookIDCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("categoryID")) {
                            categoryIDCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookName")) {
                            bookNameCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookLink")) {
                            bookLinkCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookThumb")) {
                            bookThumbCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookImg")) {
                            bookImgCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookPrice")) {
                            bookPriceCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookPages")) {
                            bookPagesCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookFormat")) {
                            bookFormatCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookAuthor")) {
                            bookAuthorCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookPublishedDate")) {
                            bookPublishedDateCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookISBN")) {
                            bookISBNCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookDetailedEdition")) {
                            bookDetailedEditionCheck = true;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookDescription")) {
                            bookDescriptionCheck = true;
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        if (reader.getLocalName().equalsIgnoreCase("book")) {
                            foundBook = false;
                            if (matchBook) {
                                result.add(book);
                                matchBook = false;
                            }
                        } else if (reader.getLocalName().equalsIgnoreCase("bookID")) {
                            bookIDCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("categoryID")) {
                            categoryIDCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookName")) {
                            bookNameCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookLink")) {
                            bookLinkCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookThumb")) {
                            bookThumbCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookImg")) {
                            bookImgCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookPrice")) {
                            bookPriceCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookPages")) {
                            bookPagesCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookFormat")) {
                            bookFormatCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookAuthor")) {
                            bookAuthorCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookPublishedDate")) {
                            bookPublishedDateCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookISBN")) {
                            bookISBNCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookDetailedEdition")) {
                            bookDetailedEditionCheck = false;
                        } else if (reader.getLocalName().equalsIgnoreCase("bookDescription")) {
                            bookDescriptionCheck = false;
                        }
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        if (bookIDCheck) {
                            book = new Book();
                            book.setBookID(reader.getText().trim());
                        }
                        if (categoryIDCheck) {
                            if (reader.getText().trim().equalsIgnoreCase(categoryID)) {
                                matchBook = true;//Found match book
                                book.setCategoryID(categoryID);
                            }
                        }
                        if (matchBook && bookNameCheck) {
                            book.setBookName(reader.getText().trim());
                        } else if (matchBook && bookLinkCheck) {
                            book.setBookLink(reader.getText().trim());
                        } else if (matchBook && bookThumbCheck) {
                            book.setBookThumb(reader.getText().trim());
                        } else if (matchBook && bookImgCheck) {
                            book.setBookImg(reader.getText().trim());
                        } else if (matchBook && bookPriceCheck) {
                            book.setBookPrice(reader.getText().trim());
                        } else if (matchBook && bookPagesCheck) {
                            book.setBookPages(reader.getText().trim());
                        } else if (matchBook && bookFormatCheck) {
                            book.setBookFormat(reader.getText().trim());
                        } else if (matchBook && bookAuthorCheck) {
                            book.setBookAuthor(reader.getText().trim());
                        } else if (matchBook && bookPublishedDateCheck) {
                            book.setBookPublishedDate(reader.getText().trim());
                        } else if (matchBook && bookISBNCheck) {
                            book.setBookISBN(reader.getText().trim());
                        } else if (matchBook && bookDetailedEditionCheck) {
                            book.setBookDetailedEdition(reader.getText().trim());
                        } else if (matchBook && bookDescriptionCheck) {
                            book.setBookDescription(reader.getText().trim());
                        }
                        break;
                }
                reader.next();
            }

            int eventType = reader.getEventType();
            if (eventType == XMLStreamConstants.END_DOCUMENT) {

                reader.close();

            }
            System.out.println("USING StAX PARSER");
            return result;
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            Logger.getLogger(StAXParserAPI.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                inputFile.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
